package com.shop.shopserver.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shopcommon.context.SecurityContext;
import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.PayDTO;
import com.shop.shoppojo.entity.Cart;
import com.shop.shoppojo.entity.Goods;
import com.shop.shoppojo.entity.User;
import com.shop.shoppojo.entity.UserOrder;
import com.shop.shoppojo.vo.UnSubmittedOrderSGoodsVO;
import com.shop.shoppojo.vo.UnSubmittedOrdersDataVO;
import com.shop.shoppojo.vo.UnSubmittedOrdersOrdersVO;
import com.shop.shoppojo.vo.UnSubmittedOrdersPersonalVO;
import com.shop.shopserver.mapper.CartMapper;
import com.shop.shopserver.mapper.PayMapper;
import com.shop.shopserver.mapper.GoodsMapper;
import com.shop.shopserver.mapper.UserMapper;
import com.shop.shopserver.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PayServiceImpl extends ServiceImpl<PayMapper, UserOrder> implements PayService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 购物车结算
     * @param payDTO
     * @return
     */

    //购物车跳转结算 根据cartIds操作
    public Result<UnSubmittedOrdersDataVO> goPayCart(PayDTO payDTO){
        log.info("购物车进入结算");
        Integer totalNum=0;//记录该订单共多少商品
        BigDecimal totalPrice= BigDecimal.valueOf(0);
        String[] cartIdsArray = payDTO.getCartIds().split(",");
        List<UnSubmittedOrderSGoodsVO> unSubmittedOrdersGoodsList=new ArrayList<>();
        for (String s : cartIdsArray) {
            //查找id对应的购物车信息
            Cart cart = cartMapper.selectOne(new LambdaQueryWrapper<Cart>().eq(Cart::getId, s));
            log.info("cart" + cart);
            //根据goods_id查找商品信息
            Goods goods = goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, cart.getGoodsId()));
            log.info("goods" + goods);
            //设置UnSubmittedOrdersGoods
            UnSubmittedOrderSGoodsVO unSubmittedOrderSGoodsVO = UnSubmittedOrderSGoodsVO
                    .builder()
                    .goodsId(goods.getId())
                    .goodsName(goods.getGoodsName())
                    .goodsImage(goods.getGoodsImage())
                    .totalNum(cart.getGoodsNum())
                    .totalPayPrice((BigDecimal.valueOf(Math.round(cart.getGoodsNum() * Double.parseDouble(goods.getGoodsPriceMin().toString()) * 1000)/ 1000.0)).setScale(2))
                    .build();
            //设置goodsList追加商品
            unSubmittedOrdersGoodsList.add(unSubmittedOrderSGoodsVO);
            //计算商品总数量和总价格
            totalNum += cart.getGoodsNum();
            totalPrice = (BigDecimal.valueOf(Double.parseDouble(totalPrice.toString())+Math.round(cart.getGoodsNum() * Double.parseDouble(goods.getGoodsPriceMin().toString()) * 1000) / 1000.0)).setScale(2);
            log.info("数量为" + cart.getGoodsNum());
            log.info("totalPrice目前为" + totalPrice);
        }
        //设置order
        UnSubmittedOrdersOrdersVO unSubmittedOrdersOrdersVO= UnSubmittedOrdersOrdersVO
                .builder()
                .goodsList(unSubmittedOrdersGoodsList)
                .orderTotalNum(totalNum)
                .orderTotalPrice(BigDecimal.valueOf(Math.round(Double.parseDouble(totalPrice.toString())*1000)/1000.0))
                .build();
        //设置personal
        User user=userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, getUserIdByUsername().getId()));
        UnSubmittedOrdersPersonalVO unSubmittedOrdersPersonalVO=new UnSubmittedOrdersPersonalVO();
        unSubmittedOrdersPersonalVO.setBalance(user.getPayMoney());
        //设置Data
        UnSubmittedOrdersDataVO unSubmittedOrdersDataVO = UnSubmittedOrdersDataVO
                .builder()
                .order(unSubmittedOrdersOrdersVO)
                .personal(unSubmittedOrdersPersonalVO)
                .build();

        //返回
        return Result.success(unSubmittedOrdersDataVO,"返回成功");
    }

    /**
     * 直接结算
     * @param payDTO
     * @return
     */

    //直接购买 根据goodsId操作
    public Result<UnSubmittedOrdersDataVO> goPayProDetail(PayDTO payDTO){
        log.info("直接购买进入结算");
        Integer totalNum=0;//记录该订单共多少商品
        BigDecimal totalPrice= BigDecimal.valueOf(0);
        List<UnSubmittedOrderSGoodsVO> unSubmittedOrdersGoodsList=new ArrayList<>();
        //根据goods_id查找商品信息
        Goods goods=goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, payDTO.getGoodsId()));
        //设置UnSubmittedOrdersGoods
        UnSubmittedOrderSGoodsVO unSubmittedOrderSGoodsVO= UnSubmittedOrderSGoodsVO
                .builder()
                .goodsId(goods.getId())
                .goodsName(goods.getGoodsName())
                .goodsImage(goods.getGoodsImage())
                .totalNum(payDTO.getGoodsNum())
                .totalPayPrice((BigDecimal.valueOf(Math.round(payDTO.getGoodsNum()*Double.parseDouble(goods.getGoodsPriceMin().toString())*1000)/1000.0)).setScale(2))
                .build();

        //设置goodsList追加商品
        unSubmittedOrdersGoodsList.add(unSubmittedOrderSGoodsVO);
        //计算商品总数量和总价格
        totalNum= payDTO.getGoodsNum();
        totalPrice= (BigDecimal.valueOf(payDTO.getGoodsNum()*Double.parseDouble(goods.getGoodsPriceMin().toString()))).setScale(2);
        //设置order
        UnSubmittedOrdersOrdersVO unSubmittedOrdersOrders= UnSubmittedOrdersOrdersVO
                .builder()
                .goodsList(unSubmittedOrdersGoodsList)
                .orderTotalNum(totalNum)
                .orderTotalPrice(totalPrice)
                .build();

        //设置personal
        User user=userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId,getUserIdByUsername().getId()));
        UnSubmittedOrdersPersonalVO unSubmittedOrdersPersonal=new UnSubmittedOrdersPersonalVO();
        unSubmittedOrdersPersonal.setBalance(user.getPayMoney());
        //设置Data
        UnSubmittedOrdersDataVO unSubmittedOrdersDataVO = UnSubmittedOrdersDataVO
                .builder()
                .order(unSubmittedOrdersOrders)
                .personal(unSubmittedOrdersPersonal)
                .build();

        //返回
        return Result.success(unSubmittedOrdersDataVO,"返回成功");
    }

    /**
     * 根据用户名查询用户id
     * @return
     */
    private User getUserIdByUsername() {
        String username = SecurityContext.getUsername();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .select(User::getId)
                .eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("user" + user);
        return user;
    }
}
